package com.example.tilitili.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

@Component
public class MD5Util {
    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    /*一次md5加密，固定salt*/
    private static final String salt = "1abv4k76e";

    public static String inputPassToFormPass(String inputPass) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + inputPass + salt.charAt(6) + salt.charAt(3);
        return md5(str);
    }

    /*二次md5加密，随机salt，存入数据库*/
    public static String formPassToDBPass(String formPass, String salt) {
        String str = "" + salt.charAt(1) + salt.charAt(4) + formPass + salt.charAt(7) + salt.charAt(5);
        return md5(str);
    }

    /*方法调用*/
    public static String inputPassToDbPass(String inputpass, String saltDB) {
        String formPass = MD5Util.inputPassToFormPass(inputpass);
        return MD5Util.formPassToDBPass(formPass, saltDB);
    }
    /*测试*/
    public static void main(String[] args) {
        System.out.println(inputPassToFormPass("123456"));
        System.out.println(formPassToDBPass(inputPassToFormPass("123456"),"1abv4k76e"));
        System.out.println(inputPassToDbPass("123456", "1abv4k76e"));
    }
}
